package com.st.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.st.enity.User;
import com.st.service.IUserService;
import com.st.service.impl.UserServiceImpl;

@WebServlet("/user.do")
public class UserServlet extends BaseServlet {
	private IUserService userService = new UserServiceImpl();
	public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		session.invalidate();
	
		response.sendRedirect("index.jsp");
	}

	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		//1.获取参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User loginuser = new User();
		loginuser.setUsername(username);
		loginuser.setPassword(password);
		//2.处理业务
		User user = userService.login(loginuser);
		//3.返回结果
		if (user != null) {
			//登录成功
			//保存登录状态
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			System.out.println(session);
			//跳转到主页
			response.sendRedirect("index.jsp");
		}else {
			request.setAttribute("error", "账号或密码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
}
